package com.clearance.mine_clearance.entry;

import com.alibaba.fastjson.JSONArray;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.clearance.mine_clearance.mapper.BetInfoMapper;
import com.clearance.mine_clearance.mapper.CapitalMapper;
import com.clearance.mine_clearance.mapper.TgUserNapper;
import com.clearance.mine_clearance.service.impl.KeyboardMarkupImpl;
import com.baomidou.mybatisplus.annotation.TableName;
import com.clearance.mine_clearance.config.Constants;
import com.clearance.mine_clearance.untils.BeanUtils;
import com.clearance.mine_clearance.untils.MinesUntils;
import com.clearance.mine_clearance.untils.RedisUntils;
import com.clearance.mine_clearance.untils.SendMessageUntils;
import com.fasterxml.jackson.annotation.JsonFormat;
import lombok.Data;
import org.springframework.context.annotation.Bean;
import org.springframework.format.annotation.DateTimeFormat;
import org.telegram.telegrambots.meta.api.methods.ParseMode;
import org.telegram.telegrambots.meta.api.methods.send.SendPhoto;
import org.telegram.telegrambots.meta.api.methods.updatingmessages.EditMessageCaption;
import org.telegram.telegrambots.meta.api.methods.updatingmessages.EditMessageText;
import org.telegram.telegrambots.meta.api.objects.CallbackQuery;
import org.telegram.telegrambots.meta.api.objects.Message;
import org.telegram.telegrambots.meta.api.objects.replykeyboard.InlineKeyboardMarkup;
import org.telegram.telegrambots.meta.api.objects.replykeyboard.buttons.InlineKeyboardButton;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import java.util.List;

@Data
@TableName("redPacket")
public class RedPacket {


    @TableField(exist = false)
    private TgUserNapper tgUserNapper;
    @TableField(exist = false)
    private CapitalMapper capitalMapper;

    {
        tgUserNapper = (TgUserNapper) BeanUtils.getBean("tgUserNapper");
        capitalMapper = (CapitalMapper) BeanUtils.getBean("capitalMapper");
    }

    @TableId(type = IdType.AUTO)
    private Integer id;

    //金额
    private BigDecimal amount;

    //倍数
    private BigDecimal mulriple;

    //Tg用户ID
    private Integer tgUserId;

    //中雷数
    private Integer mines;

    //红包总数;
    private Integer redPacketSum;

    //已抢
    private Integer rob;

    //数据数组;
    private String dataAmount;

    //索引;
    private Integer dataIndex;


    private Integer diceValue;

    //消息ID;
    private Integer messageId;

    //群组ID
    private String chatId;

    //中雷盈利
    @TableField("zhonglei_profit")
    private BigDecimal zhongleiProfit;


    //佣金金额
    @TableField("commission_income")
    private BigDecimal commissionIncome;

    //代理
    @TableField("agency_income")
    private BigDecimal agencyIncome;


    @TableField("actual_arrival")
    //实际到账
    private BigDecimal actualArrival;

    @DateTimeFormat(pattern = "yyyy-MM-dd hh:mm:ss")
    @JsonFormat(pattern = "yyyy-MM-dd hh:mm:ss")
    @TableField("time")
    private Date time;

    //6.[💣] 347.66U
    //4.[🥇] 52.04U
    //抢包信息;
    public EditMessageCaption getEditMessageCaption(TgUser tgUser, List<BetInfo> betInfoList, boolean flag) {
        EditMessageCaption editMessageCaption = new EditMessageCaption();
        editMessageCaption.setMessageId(messageId);
        editMessageCaption.setChatId(chatId);
        String initText = getInitText(tgUser, flag);
        //查询历史开奖信息;
        Collections.shuffle(betInfoList);
        if (!flag) {
            initText = initText + " \n\n---------领取详情---------\n";
            if (betInfoList != null && betInfoList.size() > 0) {
                for (int i = 1; i <= betInfoList.size(); i++) {
                    BetInfo betInfo = betInfoList.get(i - 1);
                    //0是1否
                    if (betInfo.getIsRadar() == 0) {
                        //中雷
                        initText = initText + "<code>" + i + ".[\uD83D\uDCA3]" + "+ 【" + betInfo.getAmount().stripTrailingZeros().toPlainString() + "U】 </code>" + "<code>" + betInfo.getFirstName() + "</code>\n";
                    } else {
                        initText = initText + "<code>" + i + ".[\uD83D\uDCB5]" + "+ 【" + betInfo.getAmount().stripTrailingZeros().toPlainString() + "U】 </code>" + "<code>" + betInfo.getFirstName() + "</code>\n";
                    }
                }
            }
            String gameResult = "";
            for (BetInfo betInfo : betInfoList) {
                String toString = betInfo.getAmount().stripTrailingZeros().toPlainString();
                String substring = toString.substring(toString.length() - 1);
                gameResult = gameResult + substring + "-";
            }
            gameResult = gameResult.substring(0,gameResult.length() - 1);
            //盘算是否有中雷的;
            if(actualArrival.doubleValue()>amount.doubleValue()){
                initText = initText + "\n\n\uD83D\uDCB9中雷盈利: " + zhongleiProfit.stripTrailingZeros().toPlainString() + "\n" +
                        "\uD83D\uDCB9发包成本: -" + amount.stripTrailingZeros().toPlainString() + "\n" +
                        "\uD83D\uDCB9包主实收: " + actualArrival.subtract(amount).subtract(amount).stripTrailingZeros().toPlainString() + "\n" +
                        "\uD83D\uDCB9开奖结果: " + gameResult;
            }else {
                initText = initText + "\n\n\uD83D\uDCB9中雷盈利: " + zhongleiProfit.stripTrailingZeros().toPlainString() + "\n" +
                        "\uD83D\uDCB9发包成本: -" + amount.stripTrailingZeros().toPlainString() + "\n" +
                        "\uD83D\uDCB9包主实收: " + actualArrival.stripTrailingZeros().toPlainString() + "\n" +
                        "\uD83D\uDCB9开奖结果: " + gameResult;
            }

        }
        editMessageCaption.setCaption(initText);
        editMessageCaption.setParseMode(ParseMode.HTML);
        InlineKeyboardMarkup inCommonUseMsgKeyWord = getInCommonUseMsgKeyWord(flag, tgUser);
        editMessageCaption.setReplyMarkup(inCommonUseMsgKeyWord);
        return editMessageCaption;
    }

    public String getInitText(TgUser tgUser, boolean flag) {
        String resultMessage;
        if (flag) {
            resultMessage = "用户 【"+tgUser.getFirstName()+"】发送了" + redPacketSum + "个红包，快来抢! \n" +
                    "\uD83E\uDDE7红包金额: " + amount.stripTrailingZeros().toPlainString() + "USDT\n" +
                    "\uD83D\uDECE红包包数: " + redPacketSum + "\n" +
                    "\uD83D\uDCA5红包倍数: " + mulriple.stripTrailingZeros().toPlainString() + "\n" +
                    "\uD83D\uDCA5中雷数字: " + mines;
        } else {
            resultMessage = "用户 【"+tgUser.getFirstName()+"】的红包已被抢完!\n" +
                    "\uD83E\uDDE7红包金额: " + amount.stripTrailingZeros().toPlainString() + "USDT\n" +
                    "\uD83D\uDECE红包包数: " + redPacketSum + "\n" +
                    "\uD83D\uDCA5红包倍数: " + mulriple.stripTrailingZeros().toPlainString() + "\n" +
                    "\uD83D\uDCA5中雷数字: " + mines;
        }
        return resultMessage;

    }

    //发包信息;
    public SendPhoto getSendPhotoMessage(TgUser tgUser) {
        String text = getInitText(tgUser, true);
        Config config = RedisUntils.getConfig();
        SendPhoto sendPhoto = SendMessageUntils.initSendPhoto(config.getGroupChatId(), text, config.getUrl());
        sendPhoto.setReplyMarkup(getInitInCommonUseMsgKeyWord(tgUser));
        return sendPhoto;
    }

    public InlineKeyboardMarkup getInitInCommonUseMsgKeyWord(TgUser tgUser) {
        List<ButtenConfig> buttenConfig = RedisUntils.getButtenConfig();
        ButtenConfig buttenConfig1 = buttenConfig.get(0);
        ButtenConfig buttenConfig2 = buttenConfig.get(1);
        ButtenConfig buttenConfig3 = buttenConfig.get(2);
        KeyboardMarkupImpl keyboardMarkup = new KeyboardMarkupImpl();
        List<InlineKeyboardButton> oneRow = new ArrayList<>();
        List<InlineKeyboardButton> twoRow = new ArrayList<>();
        List<InlineKeyboardButton> threeRow = new ArrayList<>();
        List<InlineKeyboardButton> foreRow = new ArrayList<>();
        InlineKeyboardButton bothway = keyboardMarkup.getInlineKeyboardButton("\uD83E\uDDE7抢红包【" + dataIndex + "/" + redPacketSum + "】总" + amount + "U\uD83D\uDCA3雷" + mines, null);
        InlineKeyboardButton topUpOrEithdraw = keyboardMarkup.getInlineKeyboardButton(buttenConfig1.getButtenName(), buttenConfig1.getButtenUrl());
        InlineKeyboardButton bill = keyboardMarkup.getInlineKeyboardButton(buttenConfig2.getButtenName(), buttenConfig2.getButtenUrl());
        InlineKeyboardButton wafa = keyboardMarkup.getInlineKeyboardButton(buttenConfig3.getButtenName(), buttenConfig3.getButtenUrl());
        InlineKeyboardButton myAmount = keyboardMarkup.getInlineKeyboardButton(Constants.balance, null);
        InlineKeyboardButton promotionLink = keyboardMarkup.getInlineKeyboardButton(Constants.promotionLink, null);
        InlineKeyboardButton profitAndLoss = keyboardMarkup.getInlineKeyboardButton(Constants.promotionSelect, null);
        InlineKeyboardButton statement = keyboardMarkup.getInlineKeyboardButton(Constants.toDayStatement, null);
        InlineKeyboardButton yearDayStatement = keyboardMarkup.getInlineKeyboardButton(Constants.yearDayStatement, null);
        bothway.setCallbackData(Constants.redPacket + id);
        myAmount.setCallbackData(Constants.MYAMOUNT + id);
        promotionLink.setCallbackData(Constants.promotionLink);
        profitAndLoss.setCallbackData(Constants.promotionSelect + tgUser.getId());
        statement.setCallbackData(Constants.toDayStatement + tgUser.getId());
        yearDayStatement.setCallbackData(Constants.yearDayStatement + tgUser.getId());
        oneRow.add(bothway);
        twoRow.add(topUpOrEithdraw);
        twoRow.add(bill);
        twoRow.add(wafa);
        twoRow.add(myAmount);
        threeRow.add(promotionLink);
        threeRow.add(profitAndLoss);
        foreRow.add(statement);
        foreRow.add(yearDayStatement);
        List<List<InlineKeyboardButton>> keyboard = new ArrayList<>();
        keyboard.add(oneRow);
        keyboard.add(twoRow);
        keyboard.add(threeRow);
        keyboard.add(foreRow);
        InlineKeyboardMarkup inlineKeyboardMarkup = new InlineKeyboardMarkup();
        //添加按钮行数;
        inlineKeyboardMarkup.setKeyboard(keyboard);
        return inlineKeyboardMarkup;
    }


    public InlineKeyboardMarkup getInCommonUseMsgKeyWord(boolean flag, TgUser tgUser) {
        List<ButtenConfig> buttenConfig = RedisUntils.getButtenConfig();
        ButtenConfig buttenConfig1 = buttenConfig.get(0);
        ButtenConfig buttenConfig2 = buttenConfig.get(1);
        ButtenConfig buttenConfig3 = buttenConfig.get(2);
        KeyboardMarkupImpl keyboardMarkup = new KeyboardMarkupImpl();
        InlineKeyboardButton bothway;
        if (flag) {
            bothway = keyboardMarkup.getInlineKeyboardButton("\uD83E\uDDE7抢红包【" + dataIndex + "/" + redPacketSum + "】\uD83D\uDCA3" + mines, null);
            bothway.setCallbackData(Constants.redPacket + id);
        } else {
            bothway = keyboardMarkup.getInlineKeyboardButton("\uD83E\uDDE7抢红包【" + dataIndex + "/" + redPacketSum + "】\uD83D\uDCA3" + mines, null);
            bothway.setCallbackData(Constants.redPacket + id);
        }
        List<InlineKeyboardButton> oneRow = new ArrayList<>();
        List<InlineKeyboardButton> twoRow = new ArrayList<>();
        List<InlineKeyboardButton> threeRow = new ArrayList<>();
        List<InlineKeyboardButton> foreRow = new ArrayList<>();
        InlineKeyboardButton topUpOrEithdraw = keyboardMarkup.getInlineKeyboardButton(buttenConfig1.getButtenName(), buttenConfig1.getButtenUrl());
        InlineKeyboardButton bill = keyboardMarkup.getInlineKeyboardButton(buttenConfig2.getButtenName(), buttenConfig2.getButtenUrl());
        InlineKeyboardButton wafa = keyboardMarkup.getInlineKeyboardButton(buttenConfig3.getButtenName(), buttenConfig3.getButtenUrl());
        InlineKeyboardButton myAmount = keyboardMarkup.getInlineKeyboardButton(Constants.balance, null);
        InlineKeyboardButton promotionLink = keyboardMarkup.getInlineKeyboardButton(Constants.promotionLink, null);
        InlineKeyboardButton profitAndLoss = keyboardMarkup.getInlineKeyboardButton(Constants.promotionSelect, null);
        InlineKeyboardButton statement = keyboardMarkup.getInlineKeyboardButton(Constants.toDayStatement, null);
        InlineKeyboardButton yearDayStatement = keyboardMarkup.getInlineKeyboardButton(Constants.yearDayStatement, null);
        myAmount.setCallbackData(Constants.MYAMOUNT + id);
        promotionLink.setCallbackData(Constants.promotionLink);
        profitAndLoss.setCallbackData(Constants.promotionSelect + tgUser.getId());
        statement.setCallbackData(Constants.toDayStatement + tgUser.getId());
        yearDayStatement.setCallbackData(Constants.yearDayStatement + tgUser.getId());
        if (flag) {
            oneRow.add(bothway);
        }
        twoRow.add(topUpOrEithdraw);
        twoRow.add(bill);
        twoRow.add(wafa);
        twoRow.add(myAmount);
        threeRow.add(promotionLink);
        threeRow.add(profitAndLoss);
        foreRow.add(statement);
        foreRow.add(yearDayStatement);
        List<List<InlineKeyboardButton>> keyboard = new ArrayList<>();
        if (flag) {
            keyboard.add(oneRow);
        }
        keyboard.add(twoRow);
        keyboard.add(threeRow);
        keyboard.add(foreRow);
        InlineKeyboardMarkup inlineKeyboardMarkup = new InlineKeyboardMarkup();
        //添加按钮行数;
        inlineKeyboardMarkup.setKeyboard(keyboard);
        return inlineKeyboardMarkup;
    }
}
























